﻿app.controller("ngAdminController", function ($http, $scope, $window, $q, LoginService, $location, itemfactory) {
    $scope.images = [];
    $scope.item;
    $scope.adminDash = true;
    $scope.allitems = [];

    $http.get("api/admin/allitems").success(function (data) {
        for (var i in data) {
            if (data[i].IsActive) {
                data[i].class = "adminAllItemsActive";
            } else {
                data[i].class = "adminAllItemsInactive";
            }
            $scope.allitems.push(data[i]);
            console.log(data[i]);
        }
    }).error(function (data) {
        console.log(JSON.stringify(data));
    });

    $scope.addImage = function () {
        $scope.images.push({url:""});
    };
    $scope.SaveItem = function (item) {
        item.imageUrl = [];
        for (var i in $scope.images) {
            item.imageUrl.push($scope.images[i].url)
        }
        console.log(item);
        $http.post("/api/item/", item).success(function (data) {
            item.Name = "";
            item.Description = "";
            item.Category = "";
            item.imageUrl = [];
            $scope.images = [];
        }).error(function (data) {
            console.log(JSON.stringify(data))
        });
    };
    $scope.SendMessage = function (email) {
        console.log(email);
    };
    $scope.EditItem = function (item) {
        console.log(item);
        $http.put("api/item/", item).success(function (data) {
            item = data;
            //$scope.showEdit = false;
        }).error(function (data) {
            $scope.error = "Error, unable to save." + data;
        });
    };
    $scope.DeleteItem = function (item) {
        $http.delete("api/item/delete/" + item.Id).success(function (data) {
            alert("Delete Successful!");
            
            for (var i in $scope.allitems) {
                if (item.Id === $scope.allitems[i].Id) {
                    $scope.allitems.splice(i, 1);
                    break;
                }
            };
        }).error(function (data) {
            $scope.error = "Error cannot delete!" + data;
        });
    };
});